import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:wechat_demo/Scenes/Discover/discover_detail_page.dart';

class DiscoverCell extends StatefulWidget {
  const DiscoverCell({
    super.key,
    required this.leftIcon,
    required this.leftTitle,
    this.showRed = false,
    this.rightTitle = '',
  });

  final Icon leftIcon;
  final String leftTitle;
  final bool showRed;
  final String rightTitle;

  @override
  State<DiscoverCell> createState() => _DiscoverCellState();
}

class _DiscoverCellState extends State<DiscoverCell> {
  Color _bgColor = Colors.white;

  void _changeBgColor(Color color) {
    setState(() {
      _bgColor = color;
    });
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        if (kDebugMode) {
          var title = widget.leftTitle;
          print("tap ${widget.leftTitle}");
        }
        _changeBgColor(Colors.white);
        Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) =>
                    DiscoverDetailPage(pageTile: widget.leftTitle)));
      },
      onTapDown: (TapDownDetails details) {
        _changeBgColor(Colors.grey);
      },
      onTapCancel: () {
        _changeBgColor(Colors.white);
      },
      child: Container(
        color: _bgColor,
        padding: const EdgeInsets.only(left: 20, right: 20),
        height: 50,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            DiscoverCellLeft(
                leftIcon: widget.leftIcon, leftTitle: widget.leftTitle),
            DiscoverCellRight(
              showRed: widget.showRed,
              rightTitle: widget.rightTitle,
            ),
          ],
        ),
      ),
    );
  }
}

class DiscoverCellLeft extends StatelessWidget {
  const DiscoverCellLeft({
    super.key,
    required this.leftIcon,
    required this.leftTitle,
  });
  final Icon leftIcon;
  final String leftTitle;

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        leftIcon,
        Text(leftTitle),
      ],
    );
  }
}

class DiscoverCellRight extends StatelessWidget {
  const DiscoverCellRight({
    super.key,
    this.showRed = false,
    required this.rightTitle,
  });

  final bool showRed;
  final String rightTitle;

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        (rightTitle.isNotEmpty ? Text(rightTitle) : const Text('')),
        const Icon(
          Icons.arrow_right,
          size: 15,
        ),
      ],
    );
  }
}
